﻿using System;
using System.Net;
using ObjectSync.Interfaces;

namespace ObjectSync
{
    public sealed class NetworkManager
    {
        #region SINGLETON
        static readonly NetworkManager instance = new NetworkManager();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static NetworkManager()
        {
        }

        NetworkManager()
        {
        }

        public static NetworkManager Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion

        public ICommunicator Communicator { get; private set; }

        /// <summary>
        /// Start operations as a server
        /// </summary>
        /// <param name="port">Number representing a udp port</param>
        public void StartServer(Int32 port)
        {
			int p = port;
            Communicator = new ServerCommunicator(p);
        }

        /// <summary>
        /// Start operations as a client conected to the specified server
        /// </summary>
        /// <param name="server">IpAddress of the server</param>
        /// <param name="port"></param>
        public void StartClient(string serverAddress, int port)
        {
            Communicator = new ClientCommunicator(serverAddress, port);

			while(NetworkManager.Instance.Communicator.getStatus() == comStatus.diconected);
            
        }
    }
}
